package com.sghj.SysPermission.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sghj.SysPermission.entity.SysMenu;
import com.sghj.SysPermission.entity.SysUser;
import com.sghj.SysPermission.mapper.SysMenuMapper;
import com.sghj.SysPermission.service.SysMenuService;
import com.sghj.SysPermission.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xrx
 * @since 2022-02-18
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    private SysUserService userService;

    /**
     * 获取用户的所有权限
     * @param sysUserId
     * @return
     */
    @Override
    public Set<SysMenu> getUserMenu(Integer sysUserId) {
        Set<SysMenu> menuSet = baseMapper.getUserMenu(sysUserId);

        Set<SysMenu> finalMenu = new HashSet<>();
        for (SysMenu sysMenu : menuSet) {
            if (sysMenu.getType().equals("2")){
                finalMenu.add(sysMenu);
            }

            List<SysMenu> childrenList = new ArrayList<>();
            for (SysMenu menu : menuSet) {
                if (sysMenu.getMenuId().equals(menu.getParentId())){
                    childrenList.add(menu);
                }
            }
            sysMenu.setChildren(childrenList);
        }
        return finalMenu;
    }

    /**
     * 获取用户全部权限
     * @param
     * @return
     */
    @Override
    public List<SysMenu> selectMenuTreeByUserId() {
        //获取当前用户
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser)currentUser.getPrincipal();
        if (user==null){
            throw new UnauthenticatedException();
        }
        List<SysMenu> menus = null;
        //如果是超管，给所有权限
        if (user.getUserType().equals("1")) {
            menus = baseMapper.selectMenuTreeAll();
        }
        else {
            menus = baseMapper.selectMenuTreeByUserId(user.getSysUserId());
        }

        return getChildPerms(menus, 0);
    }

    /**
     * 查询菜单树
     * @return
     */
    @Override
    public List<SysMenu> getMenuTree() {
        List<SysMenu> menus = baseMapper.selectMenuTreeAll();

        return getChildPerms(menus, 0);
    }

    /**
     * 根据父节点的ID获取所有子节点
     * @param list
     * @param parentId
     * @return
     */
    private List<SysMenu> getChildPerms(List<SysMenu> list, int parentId) {
        List<SysMenu> returnList = new ArrayList<SysMenu>();
        for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext();)
        {
            SysMenu t = (SysMenu) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId)
            {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     * @param list
     * @param t
     */
    private void recursionFn(List<SysMenu> list, SysMenu t) {
        // 得到子节点列表
        List<SysMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysMenu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     * @param list
     * @param tChild
     * @return
     */
    private boolean hasChild(List<SysMenu> list, SysMenu tChild) {
        return getChildList(list, tChild).size() > 0;
    }

    /**
     * 得到子节点列表
     * @param list
     * @param t
     * @return
     */
    private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t) {
        List<SysMenu> tlist = new ArrayList<SysMenu>();
        Iterator<SysMenu> it = list.iterator();
        while (it.hasNext())
        {
            SysMenu n = (SysMenu) it.next();
            if (n.getParentId().longValue() == t.getMenuId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    @Override
    public List<SysMenu> getMenuTreeBackEnd(){
        List<SysMenu> menus = baseMapper.selectMenuTreeAllBackEnd();

        return getChildPerms(menus, 0);
    }

    @Override
    public List<SysMenu> getMenuTreeFront(){
        List<SysMenu> menus = baseMapper.selectMenuTreeAllFront();

        return getChildPerms(menus, 0);

    }
    @Override
    public   List<SysMenu> selectMenuTreeByUserIdFront(Integer memberid){
        //获取当前用户
//        Subject currentUser = SecurityUtils.getSubject();
//       SysUser user = (SysUser)currentUser.getPrincipal();
//        MemberInfo user = (MemberInfo)currentUser.getPrincipal();
//        if (user==null){
//            throw new UnauthenticatedException();
//        }
        List<SysMenu> menus = null;
        //如果是超管，给所有权限
//        menus = baseMapper.selectMenuTreeByUserId(user.getSysUserId());
        menus = baseMapper.selectMenuTreeByUserIdFront(memberid);

        return getChildPerms(menus, 0);
    }
}
